<?xml version="1.0" encoding="UTF-8"?> 
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %> 
<%@ page import="java.util.*"%> 
<%@ page import="org.apache.juli.logging.Log" %>
<%@ page import="org.apache.juli.logging.LogFactory"%>
<%@ page import = "com.tomato.dao.PMF" %>
<%@ page import = "javax.jdo.PersistenceManager" %>
<%@ page import = "javax.jdo.Query" %>
<%@ page import = "java.util.List" %>
<%@ page import = "com.tomato.bean.Ebook" %>
<%@ page import = "com.google.appengine.api.datastore.Blob" %>
<%@ page import = "java.text.SimpleDateFormat" %>

<%
	request.setCharacterEncoding("UTF-8");
	final Log log = LogFactory.getLog("");
	String condition = request.getParameter("condition"); 
	int hit = 0;
	Calendar cal = new GregorianCalendar();
    cal.add(Calendar.DATE, -7);
	Date date = cal.getTime();
	SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
	String strDate = df.format(date);
	log.info("여기왔나");
	log.info("오셨나요?"+strDate+condition);
	int j = 0;
	int result = -1; 
	int result1 = 0; 
	PersistenceManager pm = PMF.get().getPersistenceManager();
	Query query = pm.newQuery(Ebook.class);
	%>
	<prov>
<%
	if(condition.equals("count")){
		query.setOrdering("count desc");
		query.setRange(0, 10);
	
	@SuppressWarnings("unchecked")
	List<Ebook> emp = 	(List<Ebook>)query.execute();
	for(Ebook e : emp){
			e.getId();
			e.getAuthor();
			e.getDescription();
			e.getStock();
			e.getCount();
			e.getGenre();
			e.getTitle();
			e.getImage();

%> 
<lists> 
<data> 
<id><%= e.getId() %></id>
<title><%= e.getTitle() %></title>
<author><%= e.getAuthor() %></author>
<% if(e.getImage() != null){ %>
<imageurl><%=request.getServerName()%>/e_book?method=show@amp;id=<%=e.getId()%></imageurl>
<% }else {%>
<imageurl>No image</imageurl>
<% } %>

<description><%= e.getDescription() %></description>
<stock><%= e.getStock() %></stock>
<genre><%= e.getGenre() %></genre>
<hit><%= e.getCount() %></hit>
</data>
</lists>
	<% } condition = null;%>

<%  
	}else if(condition.equals("date")){
		
		query.setOrdering("stock desc");
		@SuppressWarnings("unchecked")
		List<Ebook> emp = 	(List<Ebook>)query.execute();
		
		for(Ebook e : emp){
			if(Integer.parseInt(e.getStock()) > Integer.parseInt(strDate)){
				e.getId();
				e.getAuthor();
				e.getDescription();
				e.getStock();
				e.getCount();
				e.getGenre();
				e.getTitle();
				e.getImage();
				
	%> 
	<lists> 
	<data> 
	<id><%= e.getId() %></id>
	<title><%= e.getTitle() %></title>
	<author><%= e.getAuthor() %></author>
	<% if(e.getImage() != null){ %>
	<imageurl><%=request.getServerName()%>/e_book?method=show@amp;id=<%=e.getId()%></imageurl>
	<% }else {%>
	<imageurl>No image</imageurl>
	<% } %>

	<description><%= e.getDescription() %></description>
	<stock><%= e.getStock() %></stock>
	<genre><%= e.getGenre() %></genre>
	<hit><%= e.getCount() %></hit>
	</data>
	</lists>
			<% } %>
		<% } condition = null; %>
	<% } condition = null;%>	
</prov>
